# -*- coding:utf-8 -*-
import re,os


content='{"a_b":true,"a_dt":"2012-09-24 10:57:10.140000","a_f":1.1,"a_i":1,"a_l":9999,"a_li":[1,2,3],"a_o":{"b_bb":9,"b_list_b":["b","b"]},"a_s":"a","ii":123}'

content='{"a":"sd\pf\u4e2d\u6587","b":true,"c":false,"i":99,"li":[1,2,3],"dic1":{"ds":"dsds"}}'

coding_list=['utf-8', 'gb2312', 'gbk', 'gb18030', 'cp1252', ]
def decode_str(data):
    for coding in coding_list:
        try:
            rdata=data.decode(coding)
            return rdata
        except:
            pass
    return u''

i=0
def cnext():
    global i
    return content[i]
def get_next():
    global i
    if i<len(content):
        c=cnext()
        i+=1
        return c
    return None

end_sign='",]}:'                            #end sign

def get_name():                             #get name
    v_name=''
    while get_next()!='"':
        pass
    while True:
        c=get_next()
        if c=='"':
            break
        v_name+=c
    return v_name.strip()

def get_str():                              #get value
    v_value=''
    while True:
        c=get_next()
        if c=='"':
            break
        elif c=='\\':
            v_value+=c
            v_value+=get_next()
        else:
            v_value+=c
    return v_value

def get_bool(c):
    global i
    cc=c
    while True:
        tc=get_next()
        if tc in end_sign:
            i-=1
            break
        else:
            cc+=tc
    if cc.strip()=='true':
        return True
    elif cc.strip()=='false':
        return False
    else:
        raise Exception('bool type error in position: %d'%i)
def get_num(c):
    global i
    s_num=c
    while True:
        cc=get_next()
        if cc in end_sign:
            i-=1
            break
        else:
            s_num+=cc
    return eval(s_num.strip())
def get_list():
    global i
    r_list=[]
    while True:
        c=strip_space()
        if c=='"':
            v_value=get_str()
        elif c in 'tf':
            v_value=get_bool(c)
        elif c in '1234567890':
            v_value=get_num(c)
        elif c=='[':
            v_value=get_list()
        elif c=='{':
            v_value=get_object()
        r_list.append(v_value)
        c=strip_space()
        if c==',':
            continue
        elif c==']':
            return r_list
        else:
            raise Exception('list type error in position: %d'%i)
    
def strip_space():
    while True:
        c=get_next()
        if c!=' ':
            return c
class Object(object):
    pass
def get_object():
    global i
    obj=Object()
    while True:
        v_name=get_name()               #获得名称
        print v_name
        while get_next()!=':':          #过滤冒号
            pass
        c=get_next()
        if c=='"':
            v_value=get_str()
        elif c in 'tf':
            v_value=get_bool(c)
        elif c in '1234567890':
            v_value=get_num(c)
        elif c=='[':
            v_value=get_list()
        elif c=='{':
            v_value=get_object()
        setattr(obj,v_name,v_value)
        c=strip_space()
        if c==',':
            continue
        elif c=='}' or c==None:
            break
        else:
            print c,i
            raise Exception(c)
    return obj
print content.decode('gbk')
c=get_next()
if c=='{':
    obj=get_object()
    print '-------------------------------'
    for v in dir(obj):
        if not v.startswith('__'):
            if isinstance(getattr(obj,v),Object):
                print getattr(obj,v).ds
                pass
            else:
                print v,getattr(obj,v)











